package com.bdqn.zhang.controller;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONObject;
import com.bdqn.zhang.pojo.User;
import com.bdqn.zhang.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import util.LayData;
import util.LayDataEx;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: tao
 * @Description: talk is cheap,show me the code!
 * @Date: create in 2020/9/24 15:13
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController{

    @Resource
    private UserService userService;


    private String name;

    /*@RequestMapping("/login")
    *//*RequestParam  只写名字 代表参数是必须传的 如果不传 会抛400异常*//*
    public String login(@RequestParam(required = false,value = "username")String username,
                        @RequestParam(required = false,value = "password")String password){

        return "login";
    }*/
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }


    @RequestMapping("/login")
    /*RequestParam  只写名字 代表参数是必须传的 如果不传 会抛400*/
    public String login2(User user, Model model, HttpSession session){
        User u =  userService.login(user);
        if (u != null) {
            session.setAttribute("user",u);
            model.addAttribute("user",u);
            /*逻辑视图名*/
            return "index";
        } else {
            return "login";
        }

    }

    @RequestMapping("/list")
    @ResponseBody
    public LayDataEx getAll(){
        List<User> userList = userService.getAll();
        Integer count = userService.getCount();
        return LayDataEx.data(count,userList);
        //return new LayData(0,"",count,userList);
    }


    @RequestMapping("/toAdd")
    public String toAdd(){
        return "user/add";
    }

    @RequestMapping("/addSave")
    public String addSave(User user,Model model){
        Integer count = userService.addUser(user);
        if (count > 0) {
            return "index";
        }else {
            model.addAttribute("msg","新增失败！");
            return "user/add";
        }
    }

    @RequestMapping("/del")
    public String delUserById(@RequestParam("id") Integer id){
        return "redirect:list";
    }


    /*注销*/
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "login";
    }

    /*跳转到文件上传*/
    @RequestMapping("/toUpload")
    public String toUpload(){
        return "user/upload";
    }


    /*文件上传*/
    @RequestMapping("/upload")
    @ResponseBody
    public LayDataEx upload(MultipartFile file) throws Exception{
        FileUtil.writeBytes(file.getBytes(),"D:\\Y2\\"+ System.currentTimeMillis() + file.getOriginalFilename() );
        return LayDataEx.dataUpload(System.currentTimeMillis() + file.getOriginalFilename());
    }


}
